package cn.cs.service;

import cn.cs.common.Constants;
import cn.cs.common.EmptyUtils;
import cn.cs.mapper.CsImageMapper;
import cn.cs.pojo.CsImage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * yuexl
 */
@RestController
public class RestCsImageService {
    private static final Logger logger = LoggerFactory.getLogger(RestCsImageService.class);
    @Resource
    private CsImageMapper csImageMapper;

    @RequestMapping(value = "/getCsImageById", method = RequestMethod.POST)
    public CsImage getCsImageById(@RequestParam("id") Long id) throws Exception {
        CsImage csImage = csImageMapper.getCsImageById(id);
        return setDefaultImg(csImage);
    }

/*    @RequestMapping(value = "/getCsImageListByMap", method = RequestMethod.POST)
    public List<CsImage> getCsImageListByMap(@RequestBody Map<String, Object> param) throws Exception {
        List<CsImage> csImageList = csImageMapper.getCsImageListByMap(param);
        return setDefaultImgList(csImageList);
    }*/

    @RequestMapping(value = "/getCsImageCountByMap", method = RequestMethod.POST)
    public Integer getCsImageCountByMap(@RequestBody Map<String, Object> param) throws Exception {
        return csImageMapper.getCsImageCountByMap(param);
    }

    @RequestMapping(value = "/qdtxAddCsImage", method = RequestMethod.POST)
    public Integer qdtxAddCsImage(@RequestBody CsImage csImage) throws Exception {
        if (EmptyUtils.isNotEmpty(csImage) && EmptyUtils.isNotEmpty(csImage.getImgUrl())) {
            csImage.setCreatedTime(new Date());
            csImage.setImgUrl(csImage.getImgUrl().substring(csImage.getImgUrl().lastIndexOf("/") + 1, csImage.getImgUrl().length()));
        }
        return csImageMapper.insertCsImage(csImage);
    }

    @RequestMapping(value = "/qdtxModifyCsImage", method = RequestMethod.POST)
    public Integer qdtxModifyCsImage(@RequestBody CsImage csImage) throws Exception {
        csImage.setUpdatedTime(new Date());
        return csImageMapper.updateCsImage(csImage);
    }

    @RequestMapping(value = "/queryCsImageList", method = RequestMethod.POST)
    public List<CsImage> queryCsImageList(@RequestParam("targetId") Long targetId,
                                          @RequestParam(value = "type", required = false) Integer type,
                                          @RequestParam(value = "category", required = false) Integer category) throws Exception {
        Map<String, Object> imageParam = new HashMap<String, Object>();
        imageParam.put("targetId", targetId);
        imageParam.put("type", type);
        imageParam.put("category", category);
        List<CsImage> csImageList = csImageMapper.getCsImageListByMap(imageParam);
        return setDefaultImgList(csImageList, category, type);
    }
    /**
     * 如果图片不存在，则返回一个默认的图片
     * */
    public CsImage setDefaultImg(CsImage csImage) throws Exception {
        if (EmptyUtils.isEmpty(csImage)) {
            csImage = new CsImage();
        }
        String defaultImg = null;
        if (EmptyUtils.isEmpty(csImage.getCategory())) {
            logger.info("[setDefaultImg]" + "category为空，则设置为默认图片DEFAULT_NORMAL");
            defaultImg = Constants.DEFAULT_NORMAL;
        } else if (csImage.getCategory() == Constants.Image.ImageCategory.user) {
            logger.info("[setDefaultImg]" + "category为0，则设置为用户默认图片DEFAULT_USER");
            defaultImg = Constants.DEFAULT_USER;
        } else if (csImage.getCategory() == Constants.Image.ImageCategory.item) {
            if (csImage.getType() == Constants.Image.ImageType.normal) {
                logger.info("[setDefaultImg]" + "category为1，type为0");
                defaultImg = Constants.DEFAULT_NORMAL;
            } else if (csImage.getType() == Constants.Image.ImageType.carousel) {
                logger.info("[setDefaultImg]" + "category为1，type为1");
                defaultImg = Constants.DEFAULT_CAROUSEL;
            } else if (csImage.getType() == Constants.Image.ImageType.poster) {
                logger.info("[setDefaultImg]" + "category为1，type为2");
                defaultImg = Constants.DEFAULT_POSTER;
            }
        }
        csImage.setImgUrl(EmptyUtils.isNotEmpty(csImage.getImgUrl()) ? csImage.getImgUrl() : defaultImg);
        csImage.setImgUrl(Constants.FILE_PRE + csImage.getImgUrl());
        return csImage;
    }
    /**
     * 如果图片不存在，则返回一个默认的图片
     * */
    public List<CsImage> setDefaultImgList(List<CsImage> csImages, Integer category, Integer type) throws Exception {
        if (EmptyUtils.isEmpty(csImages)) {
            //如果没有查询到任何图片，则默认添加一个对象
            logger.info("[setDefaultImgList]" + "没有查询到任何图片");
            csImages = new ArrayList<CsImage>();
            CsImage csImage = new CsImage();
            csImage.setCategory(category);
            csImage.setType(type);
            csImages.add(csImage);
        }
        for (CsImage csImage : csImages) {
            setDefaultImg(csImage);
        }
        return csImages;
    }

    public static void main(String[] args) {
        String a = "http://192.168.2.12:8888/524314979315224576.png";
        String name = a.substring(a.lastIndexOf("/") + 1, a.length());
        System.out.println(name);
    }
}
